/**
 * 输入验证
 */
function validate(){    
   return true;
}
//保存菜单后的回调方法
function callback(data){    
   if(data=="SUCCESS"){   	 
   	  $.messager.alert("系统提示","修改用户信息成功！","info",reloadParent);
   }else if(data=="FAIL"){   	  
   	  $.messager.alert("系统提示","修改用户信息失败，请重试！","error");
   }else{
   	  $.messager.alert("系统提示","修改用户信息时系统异常："+data,"error");	
   }
}
function reloadParent(){
   parent.$('#updateWin').window('close');
   parent.reloadTable({tableId:'users'});
}
$("#user_save_btn").click(function(){
	
	var loginName = $("#loginName").val();
	var loginPwd = $("#loginPwd").val();
	var userName = $("#userName").val();
	
	if(loginName==""){
		$.messager.alert("系统提示","请输入登录名！","warning");
		return;
	}
	if(loginPwd==""){
		$.messager.alert("系统提示","请输入登录密码！","warning");
		return;
	}
	if(userName==""){
		$.messager.alert("系统提示","请输入姓名！","warning");
		return;
	}
	
	//判断手机号码
	var mobilePhone = $("#mobilePhone").val();
	if(mobilePhone != ""){
		if(!mobileValidata(mobilePhone)){
			$.messager.alert("系统提示","手机号码格式不正确！","warning");
			$("#mobilePhone").focus();
			return;
		}
	}
	
	//判断电话号码
//	var tel = $("#tel").val();
//	if(tel != ""){
//		if(!telephoneValidata(tel)){
//			$.messager.alert("系统提示","电话号码格式不正确！","warning");
//			$("#tel").focus();
//			return;
//		}
//	}
	
	//判断电子邮箱
	var email = $("#email").val();
	if(email == ""){
		$.messager.alert("系统提示","请输入电子邮箱！","warning");
		return;
	}else{
		if(!emailValidata(email)){
			$.messager.alert("系统提示","电子邮箱格式不正确！","warning");
			$("#email").focus();
			return;
		}
	}
	
	if(validate()){
		var companyId = $("#companyId").val();
		submitForm({
			formId:'saveForm',
			url:'userLoginController.do?method=updateUser&roleIds='+$("#roleIds").val()+"&companyId="+companyId,
			callback:callback
		});
	}	
});
function initCity(cityId,cityName){
   $("#citysId").combotree({
   	  width:270,
      url:'department.do?method=queryDepartment&tid=-1',
      valueField:'id',
      textField:'text',
      onBeforeExpand:function(node,param){      	 
         $('#citysId').combotree('tree').tree('options').url = "department.do?method=queryDepartment&tid="+node.id;
      },
      onLoadSuccess:function(node,data){
        $('#citysId').combotree('setText', cityName);
      },
    //获取点击的组织机构
      onClick:function(node){
    	  var isLeaf = node.leaf;
    	  if(isLeaf){
    		  $('#companyId').val(node.pid);
    	  }else{
    		  var pId = node.pid;
    		  if(pId==-1){
    			  $.messager.alert("系统提示","请选择分公司或所属部门！","warning");
    			  $('#citysId').combotree('setText', '');
    		  }else{
    			  $('#companyId').val(node.id);
    		  }
    	  }
      }
   });   
}
$(function(){
	$("#roleRefDiv").hide();
	var hidd_cityId = $("#hidd_cityId").val();
	var hidd_cityName = $("#hidd_cityName").val();
	initCity(hidd_cityId,hidd_cityName);
});


//角色参照
$("#role_ref_btn").click(function(){
	parent.$("#updateWin").panel('resize',{
	    width: 800,
//	    height: 450
	});
	 $("#mainDiv").width("50%");
	 $("#mainDiv").css("float","left");
	 $("#mainDiv").css("display","inline");
	$("#roleRefDiv").show();
	initRoleList();
})
//角色参照确定
$("#role_save_btn").click(function(){
	setRole();
})


function initRoleList(){
	dataTable({
	    tableId:'roleRefTable',
	    url:"role.do?method=queryRole",
	    idField:'roleId',
	    columns:[[
	       {field : 'roleId',checkbox : true,align : 'center'},
	       {title : '角色',field : 'roleName',align:'center',width:282}
	    ]],
	    showPage:true,
	    toolbar:'toolbar'
	});
}

function setRole(){
	var rows = $("#roleRefTable").datagrid("getChecked");
    var len = rows.length;
    if(len==0){
        $.messager.alert("系统提示","请选择要参照的数据！","warning");
        return;
    }
    var sum =  $("#sum").val()*1;
    $("#sum").val(len+sum);
    
    var roleIds = "";
    var roleNames = "";
    var str = "";
	for(var i = 0;i<len;i++){    
		var index = sum + i;
//		roleIds += rows[i].roleId + ",";
//    	roleNames += rows[i].roleName + ",";
    	str+='<tr id="trRoleDetail'+index+'"><td style="text-align:left;"><input id="role'+index+'" type="checkbox" value="'+rows[i].roleId+'"/>'+rows[i].roleName+'</td></tr>';
	}
	$("#tabRole").append(str);
	getRoleIds();
}
//删除角色
$("#delete_roleRef").click(function(){
	
	var len = $("#sum").val();
	var chooseLen = 0;
	for(var i=0;i<len;i++){
		if($("#role"+i).is(":checked")){
			chooseLen++;
		}
	}
	
	if(chooseLen==0){
		$.messager.alert("系统提示","请选择要删除的角色！","warning");
	}else{
		for(var i=0;i<len;i++){
			if($("#role"+i).is(":checked")){
				$("#trRoleDetail"+i).remove();
			}
		}
	}
	getRoleIds();
})

function closeRef(){
	$("#roleRefDiv").hide();
	parent.$("#updateWin").panel('resize',{
	    width: 400,
//	    height: 450
	});
	$("#mainDiv").width("100%");
}
//获取角色Id串
function getRoleIds(){
	var roleIds ="";
	var totalLen = $("#sum").val();
	for(var j=0;j<totalLen;j++){
		var roleId = $("#role"+j).val();
		if(typeof(roleId)!="undefined"){
			roleIds += $("#role"+j).val() + ",";
		}
	}
	$("#roleIds").val(roleIds);
}